#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_json)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/json.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "json.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int test_0(void)
{
    for (int i = 0; i < 100; i++)
    {
        if (0) 
        {
            
            #if defined (EXIT_TEST)
            exit(0);
            #endif 
            return UNITT_E_FAIL;
        }
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(test_0),
        // UNITT_TCASE(test_1),
        // UNITT_TCASE(test_2),
    };

    static UNITT suites[] = {
        { "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

#define READ_FILE "test/file/read.json"
#define WRITE_FILE "test/file/write.json"

static void json_preview(json_t json)
{
    char *text = NULL;
    text = json_dumps(json, 0, 0, NULL);
    if (text)
    {
        printf("%s\r\n", text);
        free(text);
    }
    else  
    {
        printf("[ERROR] dumps fail!!!\r\n");
    }
}

static const char *typename(int type)
{
    const char *name = NULL;

    if      (type == JSON_TYPE_UNKNOW)  name = "unknow";
    else if (type == JSON_TYPE_NULL  )  name = "null";
    else if (type == JSON_TYPE_BOOL  )  name = "bool";
    else if (type == JSON_TYPE_INT   )  name = "number int";
    else if (type == JSON_TYPE_FLOAT )  name = "number float";
    else if (type == JSON_TYPE_STRING)  name = "string";
    else if (type == JSON_TYPE_ARRAY )  name = "array";
    else if (type == JSON_TYPE_OBJECT)  name = "object";
    
    return name;
}

static void print_value(json_t json)
{
    int type = json_type(json);
    if      (type == JSON_TYPE_UNKNOW)  printf("unknow type!\r\n");
    else if (type == JSON_TYPE_NULL  )  printf("null type!\r\n");
    else if (type == JSON_TYPE_BOOL  )  printf("%s\r\n", json_value_bool(json) ? "true" : "false");
    else if (type == JSON_TYPE_INT   )  printf("%d\r\n", json_value_int(json));
    else if (type == JSON_TYPE_FLOAT )  printf("%f\r\n", json_value_float(json));
    else if (type == JSON_TYPE_STRING)  printf("%s\r\n", json_value_string(json));
    else if (type == JSON_TYPE_ARRAY )  printf("[...]\r\n");
    else if (type == JSON_TYPE_OBJECT)  printf("{...}\r\n");
}

static void test_create(void)
{
    json_t json = NULL;
    json = json_create();
    if (json)
    {
        printf("json_create success!!! %p\r\n", json);
    }
    json_delete(json);
}

static void test_add(void)
{
    json_t json = NULL;

    /* create root node */
    json = json_create();
    json_set_object(json, NULL);

    json_add_null_to_object(json, "null");
    json_add_true_to_object(json, "true");
    json_add_false_to_object(json, "false");
    json_add_bool_to_object(json, "bool", JSON_TRUE);
    json_add_int_to_object(json, "int", 178);
    json_add_float_to_object(json, "float", 12.3333);
    json_add_string_to_object(json, "string", "18");
    json_add_array_to_object(json, "array");
    json_add_object_to_object(json, "object");
    json_add_array_to_object(json, "数组");

    json_preview(json);

    json_delete(json);
}

static void test_erase(void)
{
    json_t json = NULL;

    /* create root node */
    json = json_create();
    json_set_object(json, NULL);

    json_add_null_to_object(json, "null");
    json_add_true_to_object(json, "true");
    json_add_false_to_object(json, "false");
    json_add_bool_to_object(json, "bool", JSON_TRUE);
    json_add_bool_to_object(json, "bool", JSON_FALSE);
    json_add_int_to_object(json, "int", 178);
    json_add_float_to_object(json, "float", 12.3333);
    json_add_string_to_object(json, "string", "18");
    json_add_array_to_object(json, "array");
    json_add_object_to_object(json, "object");
    json_add_array_to_object(json, "数组");

    json_preview(json);

    json_erase(json, "数组", 0); // erase the index 0 "数组"
    json_erase(json, "bool", 1); // erase the index 1 "bool"
    json_erase(json, NULL, 1); //  erase the index 1 

    json_preview(json);

    json_delete(json);
}

static void test_info(void)
{
    json_t json = NULL;
    json_t node = NULL;

    /* create root node */
    json = json_create();

    printf("json prev type: %s\r\n", typename(json_type(json)));
    json_set_object(json, NULL);
    printf("json prev type: %s\r\n", typename(json_type(json)));

    json_add_null_to_object(json, "null");
    json_add_true_to_object(json, "true");
    json_add_false_to_object(json, "false");
    json_add_bool_to_object(json, "bool", JSON_TRUE);
    json_add_int_to_object(json, "int", 178);
    json_add_float_to_object(json, "float", 12.3333);
    json_add_string_to_object(json, "string", "18");
    json_add_array_to_object(json, "array");
    json_add_object_to_object(json, "object");
    json_add_array_to_object(json, "数组");

    int size = json_size(json);
    printf("json size: %d\r\n", size);
    for (int i = 0; i < size; i++)
    {
        node = json_to_index(json, i);
        printf("node type: %s\r\n", typename(json_type(node)));
    }

    json_delete(json);
}

static void test_get(void)
{
    json_t json = NULL;
    json_t node = NULL;

    /* create root node */
    json = json_create();
    json_set_object(json, NULL);

    json_add_null_to_object(json, "null");
    json_add_true_to_object(json, "true");
    json_add_false_to_object(json, "false");
    json_add_bool_to_object(json, "bool", JSON_TRUE);
    json_add_int_to_object(json, "int", 178);
    json_add_float_to_object(json, "float", 12.3333);
    json_add_string_to_object(json, "string", "18");
    json_add_array_to_object(json, "array");
    json_add_object_to_object(json, "object");
    json_add_array_to_object(json, "数组");

    int size = json_size(json);
    for (int i = 0; i < size; i++)
    {
        node = json_to_index(json, i);

        int type = json_type(node);

        printf("key<%s> : ", json_key(node));

        if      (type == JSON_TYPE_UNKNOW)  printf("unknow type!\r\n");
        else if (type == JSON_TYPE_NULL  )  printf("null type!\r\n");
        else if (type == JSON_TYPE_BOOL  )  printf("%s\r\n", json_value_bool(node) ? "true" : "false");
        else if (type == JSON_TYPE_INT   )  printf("%d\r\n", json_value_int(node));
        else if (type == JSON_TYPE_FLOAT )  printf("%f\r\n", json_value_float(node));
        else if (type == JSON_TYPE_STRING)  printf("%s\r\n", json_value_string(node));
        else if (type == JSON_TYPE_ARRAY )  printf("[...]\r\n");
        else if (type == JSON_TYPE_OBJECT)  printf("{...}\r\n");
    }

    json_delete(json);
}

static void test_set(void)
{
    json_t json = NULL;
    json_t node = NULL;

    /* create root node */
    json = json_create();
    json_set_object(json, NULL);

    node = json_add_null_to_object(json, "null");

    json_set_bool(node, JSON_TRUE);     json_preview(json);
    json_set_bool(node, JSON_FALSE);    json_preview(json);
    json_set_int(node, 1024);           json_preview(json);
    json_set_float(node, 3.14159);      json_preview(json);
    json_set_string(node, "String");    json_preview(json);
    json_set_array(node, NULL);         json_preview(json);
    json_set_object(node, NULL);        json_preview(json);
    json_set_key(node, "new");          json_preview(json);

    json_delete(json);
}

static void test_attach(void)
{
    json_t json = NULL;
    json_t node = NULL;

    /* create root node */
    json = json_create();
    json_set_object(json, NULL);

    node = json_create_int_for_object("node", 0); json_attach(json, 0, node);
    node = json_create_int_for_object("node", 1); json_attach(json, 1, node);
    node = json_create_int_for_object("node", 2); json_attach(json, 2, node);
    node = json_create_int_for_object("node", 3); json_attach(json, 3, node);

    json_preview(json);

    node = json_detach(json, "node", 2);
    if (node)
    {
        json_delete(node);
    }

    json_preview(json);

    json_delete(json);
}

static void test_minify(void)
{
    json_t json = NULL;

    /* create root node */
    json = json_create();
    json_set_object(json, NULL);

    json_add_null_to_object(json, "null");
    json_add_true_to_object(json, "true");
    json_add_false_to_object(json, "false");
    json_add_bool_to_object(json, "bool", JSON_TRUE);
    json_add_int_to_object(json, "int", 178);
    json_add_float_to_object(json, "float", 12.3333);
    json_add_string_to_object(json, "string", "18");
    json_add_array_to_object(json, "array");
    json_add_object_to_object(json, "object");
    json_add_array_to_object(json, "数组");

    json_preview(json);

    char *text = json_dumps(json, 0, 0, NULL);
    if (text)
    {
        json_minify(text);
        printf("%s\r\n", text);
        free(text);
    }

    json_delete(json);
}

static void test_copy(void)
{
    json_t json = NULL;
    json_t copy = NULL;

    /* create root node */
    json = json_create();
    json_set_object(json, NULL);

    json_add_null_to_object(json, "null");
    json_add_true_to_object(json, "true");
    json_add_false_to_object(json, "false");
    json_add_bool_to_object(json, "bool", JSON_TRUE);
    json_add_int_to_object(json, "int", 178);
    json_add_float_to_object(json, "float", 12.3333);
    json_add_string_to_object(json, "string", "18");
    json_add_array_to_object(json, "array");
    json_add_object_to_object(json, "object");
    json_add_array_to_object(json, "数组");

    copy = json_copy(json);

    json_preview(json);
    json_preview(copy);

    json_delete(json);
    json_delete(copy);
}

static void test_load(void)
{
    json_t root = NULL, x = NULL;
    char *s = NULL;
    
    root = json_file_load(WRITE_FILE);
    if (!root)
    {
        int type = 0, line = 0, column = 0;
        type = json_error_info(&line, &column);
        printf("error at line %d column %d type %d.\r\n", line, column, type);
        return;
    }
    printf("load success!\r\n");

    x = json_to_key(root, "version");
    if (x)
    {
        if (json_isstring(x)) printf("%s\r\n", json_value_string(x));
    }

    x = json_to_index(root, 5, 0);
    printf("x type %d, %d\r\n", json_type(x), json_value_int(x));

    s = json_dumps(root, 0, 0, NULL);
    printf("%s\r\n", s);
    if (s) free(s);

    /* dump json file */
    // json_file_dump(root, WRITE_FILE);

    json_delete(root);
}

static void test_dump(void)
{
    json_t json, t, j;
    int aa[10] = {0,1,2,3,4,5,6,7,8,9};
    char *s;

    /* 创建根结点 */
    json = json_create();
    json_set_object(json, NULL);

    /* 向根节点尾部插入string和int型的两个键值对 */
    json_add_string_to_object(json, "name", "Lisi");
    json_set_array_int(json_add_null_to_object(json, "table"), aa, 10);

    json_add_null_to_object(json, "null");
    json_add_true_to_object(json, "true");
    json_add_false_to_object(json, "false");
    json_add_bool_to_object(json, "bool", JSON_TRUE);
    json_add_int_to_object(json, "int", 178);
    json_add_float_to_object(json, "float", 12.3333);
    json_add_string_to_object(json, "string", "18");
    json_add_null_to_object(json, "array");
    json_add_null_to_object(json, "object");
    json_add_array_to_object(json, "数组");
    json_attach(json, 3, json_create_float_for_object("pi", 3.14159));
    
    json_add_null_to_array(t);
    json_add_true_to_array(t);
    json_add_false_to_array(t);
    json_add_bool_to_array(t, JSON_TRUE);
    json_add_int_to_array(t, 178);
    json_add_float_to_array(t, 12.340000);
    json_add_string_to_array(t, "18");
    json_attach(t, 3, json_create_string_for_array("pi"));

    json_erase_by_index(t, 8);
    json_erase_by_key(json, "bool");

    /* get */
    t = json_to_key(json, "int");
    if (json_isint(t)) printf("%d\r\n", json_value_int(t));

    /* array */
    t = json_to_key(json, "array");
    json_set_array(t, NULL);
    json_add_null_to_array(t);
    json_add_true_to_array(t);
    json_add_false_to_array(t);
    json_attach(t, json_size(t), json_set_array(json_create(), NULL));

    /* object */
    t = json_to_key(json, "object");
    json_set_object(t, NULL);
    json_add_false_to_object(t, "1");
    json_add_int_to_object(t, "2", 15);

    /* preview json */
    s = json_dumps(json, 0, 0, NULL);
    printf("%s\r\n", s);
    if (s) free(s);

    /* dump json file */
    json_file_dump(json, WRITE_FILE);

    json_delete(json);
}

static void test_base(void)
{
    // test_dump();
    test_load();
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: json [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <ut>        Unit test\n"
"                        <create>    Test create and delete functions\n"
"                        <add>       Test add json functions\n"
"                        <erase>     Test erase json functions\n"
"                        <info>      Test get json infomations\n"
"                        <get>       Test get json key and values functions\n"
"                        <set>       Test set json key and values functions\n"
"                        <attach>    Test set attach and dettach functions\n"
"                        <minify>    Test minify functions\n"
"                        <copy>      Test copy functions\n"
"                        <dump>      Test dump functions\n"
"                        <load>      Test load functions\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -u [<period>]       Unit test period, unit ms, the default is 1000ms\n"
"\n"

    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("json version %d.%d.%d\r\n", JSON_V_MAJOR, JSON_V_MINOR, JSON_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            srand((unsigned int)time(NULL));
            #if defined(TEST_TARGET_json)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
        else if (!strcmp(execute, "create"))
        {
            test_create();
        }
        else if (!strcmp(execute, "add"))
        {
            test_add();
        }
        else if (!strcmp(execute, "erase"))
        {
            test_erase();
        }
        else if (!strcmp(execute, "info"))
        {
            test_info();
        }
        else if (!strcmp(execute, "value"))
        {
            test_get();
        }
        else if (!strcmp(execute, "set"))
        {
            test_set();
        }
        else if (!strcmp(execute, "attach"))
        {
            test_attach();
        }
        else if (!strcmp(execute, "minify"))
        {
            test_minify();
        }
        else if (!strcmp(execute, "copy"))
        {
            test_copy();
        }
        else if (!strcmp(execute, "dump"))
        {
            test_dump();
        }
        else if (!strcmp(execute, "load"))
        {
            test_load();
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_json)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_json(void)
{
    command_export("json", test);
}
init_export_app(test_json);
#endif 
